home *** CD-ROM | disk | FTP | other *** search
- Path: inforamp.net!ts26-11
- From: rmorin@inforamp.net (Randy Charles Morin)
- Newsgroups: comp.lang.c++
- Subject: Re: Encapsulation question.
- Date: Wed, 06 Mar 96 06:40:06 GMT
- Organization: MiddleWorld SoftWare
- Message-ID: <4hjbvv$elu@sam.inforamp.net>
- References: <4hg701$goi@insosf1.netins.net>
- NNTP-Posting-Host: ts26-11.tor.inforamp.net
- X-Newsreader: News Xpress Version 1.0 Beta #4
-
- In article <4hg701$goi@insosf1.netins.net>,
- hhowe@trgnet.com (Harold Howe) wrote:
- >Does the following violate encapsulation? Should an object tell a private
- >member the address of other private members? I am using this on a grander
- >scale, in which the Application class tells a private dialog object the
- >address of a private configuration structure? The dialog class needs access
- >to the structure so it can modify it, and this is how I tell the dialog where
- >to look. Aside from using globals, does anyone have any better suggestions?
-
- It's difficult to envision all the reasons you might want to do this, but in
- everycase you are violating fundamentalist (at times like this, we hate those
- red-tape guys) encapsulation rules. A better solution is to think of the
- private configuration structure as a separate object.
-
- class MyInteger
- {
- private:
- int i;
- ...
- public:
- int GetJ();
- void SetJ(int iInit);
- ...
- }
-
- class Application
- {
- private:
- MyInteger * j;
- Dialog *dlg;
- public:
- Application(void)
- { j=new MyInteger();dlg = new Dialog(&j); }
- ~Application()
- { delete j;}
- }
-
- class Dialog
- {
- private:
- MyInteger * j;
- public:
- Dialog(MyInteger * jInit) { j=jInit; } ;
- }
-
- or a simplified...
-
- class Application
- {
- private:
- int *j;
- Dialog *dlg;
- public:
- Application(void) { j=new int; dlg = new Dialog(&j); }
- ~Application() { delete j; }
- }
-
- class Dialog
- {
- private:
- int * j;
- ... // plus other members which access ptr
- public:
- Dialog(int * jInit) { j=jInit } ;
- }
-
- I hope I didn't error anywhere!
- I hope you understand it too!
- I hope it helps you, even more!
-
- Agrivar
-